package org.jeecg.modules.business.third.strategy.sap;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.plant.service.IWmsSapWarehouseMapService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.third.entity.WmsOutReceiptMaterialVo;
import org.jeecg.modules.dock.entity.dto.request.ProductReceptionRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.SaleOutboundRequestDTO;
import org.jeecg.modules.dock.entity.dto.request.SellOutboundRequestDTO;
import org.jeecg.modules.dock.entity.dto.response.ProductReceptionResponseDTO;
import org.jeecg.modules.dock.entity.dto.response.SellOutboundResponseDTO;
import org.jeecg.modules.dock.service.SapDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    调拨出库(辅材)
 * 创建人员:    hansen
 * 创建时间:    2023-12-6
 */
@Service(value = "c42SapOutbound")
@Slf4j
@RequiredArgsConstructor
public class C42SapOutbound implements SapOperateStrategy {

    private final IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;
    private final SapDockingRepeaterService sapDockingRepeaterService;

    /**
     * 调拨出库(辅材)操作
     *
     * @param map 调拨出库辅材信息
     * @return 调拨出库(辅材)操作结果列表
     */
    @Override
    public List operate(Map<String, Object> map) {
        log.info("调拨出库(辅材)");
        // 创建存放结果的列表
        List<ProductReceptionRequestDTO> list = new ArrayList<>();
        // 获取辅材信息列表
        List<WmsOutReceiptMaterialVo> wmsOutReceiptMaterialVos = (List<WmsOutReceiptMaterialVo>) map.get("records");
        // 遍历辅材信息列表
        wmsOutReceiptMaterialVos.forEach(t -> {
            // 创建ProductReceptionRequestDTO对象
            ProductReceptionRequestDTO requestDTO = new ProductReceptionRequestDTO();
            // 验证辅材信息的有效性
            SapParamUtil.validWmsOutReceiptMaterial(t);
            // 根据辅材信息查询调拨出库任务材料信息
            WmsOutReceiptTaskMaterial one = wmsOutReceiptTaskMaterialService.getOne(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>().eq(WmsOutReceiptTaskMaterial::getOriginalSystemNumber, t.getOriginalSystemNumber())
                    .eq(WmsOutReceiptTaskMaterial::getOriginalSystemLineNumber, t.getOriginalSystemLineNumber()));
            // 验证调拨出库任务记录的有效性
            SapParamUtil.validOutTaskRecord(one);
            // 将调拨出库任务材料信息转换为ProductReceptionRequestDTO对象
            ProductReceptionRequestDTO productReceptionRequestDTO = JSONObject.parseObject(one.getExtraText(), ProductReceptionRequestDTO.class);
            // 将ProductReceptionRequestDTO对象的属性值赋值给requestDTO对象的相应属性
            requestDTO.setUuid(productReceptionRequestDTO.getUuid());
            requestDTO.setFreightVoucher(productReceptionRequestDTO.getFreightVoucher());
            requestDTO.setFreightVoucherProject(productReceptionRequestDTO.getFreightVoucherProject());
            requestDTO.setSaleOrderCode(productReceptionRequestDTO.getSaleOrderCode());
            requestDTO.setSaleOrderCodeLine(productReceptionRequestDTO.getSaleOrderCodeLine());
            requestDTO.setDeliveryDate(productReceptionRequestDTO.getDeliveryDate());
            requestDTO.setUbDumpNumber(productReceptionRequestDTO.getUbDumpNumber());
            requestDTO.setUbLineProject(productReceptionRequestDTO.getUbLineProject());
            // 将ProductReceptionRequestDTO对象添加到list列表中
            list.add(requestDTO);
        });
        // 调用sapDockingRepeaterService的constructionReceiving方法，根据list列表生成调拨出库(辅材)操作结果列表
        List<ProductReceptionResponseDTO> productReceptionResponseDTOS = sapDockingRepeaterService.constructionReceiving(list);
        // 返回调拨出库(辅材)操作结果列表
        return productReceptionResponseDTOS;
    }

}
